package com.example.mybaseapp.widget.circle_seekbar

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PointF
import com.example.mybaseapp.R
import kotlin.math.abs
import kotlin.math.cos
import kotlin.math.sin
import kotlin.properties.Delegates

/**
 * 刻度盘 绘制各种元素 类
 * 绘制：背景、轮盘、指针、刻度、刻度文字
 */
class DrawaUIUtil(val context: Context) {
    val drawaUIHelper = DrawaUIHelper()

    /**指针的半径*/
    var cursorRadius = context.resources.getDimension(R.dimen.dp_12)
    /**指针中小圆的半径 */
    var cursorSmallRadius = context.resources.getDimension(R.dimen.dp_8)
    /**刻度距离圆边框的距离*/
    var scalePadding = cursorSmallRadius
    /**指针的三角形的高度*/
    var cursorSmallLen = context.resources.getDimension(R.dimen.dp_4)
    /**大刻度的长度*/
    var scaleBigLen = context.resources.getDimension(R.dimen.dp_6)
    /**刻度文字和刻度的距离*/
    var textPaddingCursor = cursorRadius
    /**整个圆的半径*/
    var circleRadius:Float = context.resources.getDimension(R.dimen.dp_360)
    /**圆心的坐标*/
    var circleCenter:PointF by Delegates.notNull()
    /**圆环和大圆的距离*/
    private var smallCircleToBigCircle = context.resources.getDimension(R.dimen.dp_44)
    /**当前指针的角度
     * 【显示的角度范围0- -180是显示的】*/
    var currentCursorDegree = -90F
    /**当前指针所在的圆心坐标*/
    var currentCursorPoint:PointF by Delegates.notNull()
    /**指针当前移动的角度*/
    var moveCursorDegree = 0F
    /**刻度移动的角度*/
    var moveScaleDegree = 0F
    /**刻度起始的旋转角度*/
    var currentScaleDegree = 0F
    /**计算文字刻度开始前的一个刻度*/
    var beforeTextStartDegree = 0F
    /**计算文字刻度结束后的一个刻度*/
    var afterTextEndDegree = 0F


    /**绘制整个圆的背景
     * 圆+圆环*/
    fun drawCircleBg(canvas: Canvas, point:PointF, radius:Float, bigPaint:Paint, smallPaint: Paint){
        canvas.drawCircle(point.x,point.y,radius, bigPaint)
        canvas.drawCircle(point.x,point.y,radius - smallCircleToBigCircle, smallPaint)
        canvas.drawCircle(point.x,point.y,radius - smallCircleToBigCircle - cursorRadius*2, bigPaint)
    }

    /**
     * 绘制指针
     * 圆+小圆+三角形（指针方向）
     */
    fun drawSmallCircle(canvas: Canvas,point:PointF,radius:Float,paint:Paint,smallPaint: Paint){
//        计算出指针的圆心坐标
//        canvas.save()
//        canvas.translate(circleCenter.x,circleCenter.y)
//        canvas.rotate(moveCursorDegree)
        currentCursorPoint = drawaUIHelper.extendPointOnCircle(point, currentCursorDegree, radius - smallCircleToBigCircle - cursorRadius)
        drawCursor(canvas,currentCursorPoint,currentCursorDegree,paint,smallPaint)
//        canvas.restore()
    }

    /**
     * 绘制指针
     * @param pointF 指针的圆心坐标
     * @param degree 指针朝向
     */
    private fun drawCursor(canvas: Canvas,pointF:PointF,degree:Float,paint: Paint,smallPaint: Paint){
        canvas.drawCircle(pointF.x,pointF.y,cursorRadius,paint)
        canvas.drawCircle(pointF.x,pointF.y,cursorSmallRadius, smallPaint)
        drawSmallDirection(canvas,pointF, degree, paint)
    }

    /**
     * 绘制指针的方向
     */
    private fun drawSmallDirection(canvas: Canvas,pointF: PointF,degree:Float,paint: Paint) {
        val points = drawaUIHelper.calculatePoints(pointF, degree, cursorRadius)
        val point = drawaUIHelper.extendPointOnCircle(pointF, degree,cursorRadius + cursorSmallLen)
        val path = Path()
        path.moveTo(points.first.x, points.first.y) // 移动到底部左侧点
        path.lineTo(points.second.x, points.second.y) // 从底部左侧点到顶部点
        path.lineTo(point.x, point.y) // 从顶部点到底部右侧点
        path.close() // 封闭路径，形成三角形
// 绘制三角形
        canvas.drawPath(path, paint)
        canvas.save()
    }

    /**
     * 初始化刻度的角度
     */
    fun refreshData(datas:MutableList<PointsData>){
        drawaUIHelper.getAllPoints(datas)
    }

    /**
     * 放手之后的操作
     */
    fun upScale(){
        currentScaleDegree = drawaUIHelper.adjustSelect(currentScaleDegree)
        println("upScale $currentScaleDegree")
    }


    /**
     * 绘制刻度
     * 从右边0度，逆时针开始绘制
     */
    fun drawScale(canvas: Canvas,pointsData: MutableList<PointsData>,bigPaint: Paint,textPaint:Paint,smallPaint: Paint){
        if(pointsData.isNotEmpty()) {
            beforeTextStartDegree =
                pointsData[0].degree - drawaUIHelper.stepSclaeDegree
            afterTextEndDegree =
                pointsData[pointsData.size - 1].degree + drawaUIHelper.stepSclaeDegree
        }
        setScaleCanvasState(canvas)
        println("当前旋转的角度：${currentScaleDegree+moveScaleDegree} ${pointsData[0].degree}")
        for(n in 0 until  drawaUIHelper.scaleSize step 2){
            drawScaleBig(canvas, -drawaUIHelper.stepSclaeDegree*n, bigPaint, textPaint ,pointsData)
            if(n+1<drawaUIHelper.scaleSize){
                drawScaleSmallDot(canvas, -drawaUIHelper.stepSclaeDegree*(n+1), smallPaint)
            }
        }
        canvas.restore()
    }

    /**绘制小的刻度
     * @param point 点的坐标，不是圆心的坐标*/
    private fun drawScaleSmallDot(canvas: Canvas,degree: Float,paint: Paint){
        val angleRadians = Math.toRadians(degree.toDouble()).toFloat()
        val pointTmpAX = (circleRadius - scalePadding) * cos(angleRadians)
        val pointTmpAY = (circleRadius - scalePadding) * sin(angleRadians)
        canvas.drawPoint(pointTmpAX,pointTmpAY,paint)
    }

    /**绘制大的刻度
     * @param point 点的坐标，不是圆心的坐标*/
    private fun drawScaleBig(canvas:Canvas,degree: Float,paint: Paint,textPaint: Paint,pointsData: MutableList<PointsData>){
        val angleRadians = Math.toRadians(degree.toDouble()).toFloat()
        val pointTmpAX = (circleRadius - scalePadding) * cos(angleRadians)
        val pointTmpAY = (circleRadius - scalePadding) * sin(angleRadians)
        val pointTmpBX = (circleRadius - scalePadding - scaleBigLen) * cos(angleRadians)
        val pointTmpBY = (circleRadius - scalePadding - scaleBigLen) * sin(angleRadians)
        canvas.drawLine(pointTmpAX, pointTmpAY,pointTmpBX,pointTmpBY,paint)


//        判断角度是否落在文字刻度之间
        if(pointsData.size!=0 && degree>beforeTextStartDegree && degree<afterTextEndDegree){
            for(d in pointsData){
                if(d.degree==degree){
                    drawText(canvas, degree,d.value.toString(),textPaint)
                }
            }
        }
    }

    /**绘制刻度文字
     * @param degree 刻度对应的角度*/
    private fun drawText(canvas: Canvas,degree: Float,value:String,paint: Paint){
        val p = drawaUIHelper.extendPointOnCircle(circleCenter,
            degree+moveScaleDegree+currentScaleDegree,
            circleRadius - scalePadding - scaleBigLen - textPaddingCursor)
        canvas.restore()
        canvas.save()
        canvas.translate(p.x,p.y)
        canvas.rotate(degree+90+moveScaleDegree+currentScaleDegree)
        canvas.drawText(value,0F,0F,paint)
        canvas.restore()
        setScaleCanvasState(canvas)
    }

    private fun setScaleCanvasState(canvas: Canvas){
        canvas.save()
        canvas.translate(circleCenter.x,circleCenter.y)
        canvas.rotate( currentScaleDegree+moveScaleDegree)
    }
}